﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;

namespace Heartbeat
{
    class Program
    {
        protected static IList<string> mChannelNames;

        static void channelsReceivedCallback(TcpClient pClient, List<String> channels)
        {
            //Subscribe to every channel.
            mChannelNames = channels;

            //Send an authentication message.
            JObject authJson = new JObject();
            authJson.Add("encodedConnectionId", "px2TxFlxS3ibHMR%2BW1uog%2FkB%2BDiMVsUZ7EGiUx0PjoqxF2O0iL9o7aFkAgUyIQjKX2vqnWRJ7Yj0B2EDsRJXUQ%3D%3D");
            pClient.SendAuthentication(authJson);
        }

        static void connectedCallback(TcpClient pClient)
        {
            Console.WriteLine("Connected: " + pClient.ToString());
        }

        static void simpleEchoCallback(TcpClient pClient, Packet pPacket)
        {
            Console.WriteLine(pClient);

            Console.WriteLine(pPacket.GetMessage());
            Console.WriteLine(pPacket.GetChannel());

            //Write the message back to the server.
            //pClient.Write(pPacket);
        }

        static void authenticationCallback(TcpClient pClient, Packet pPacket)
        {
            pClient.SubscribeTo(mChannelNames);
        }

        static void Main(string[] args)
        {
            string address = "sdm07.wv.mentorg.com";
            int port1 = 9004;
            //int port2 = 9013;
            TcpClient pClient1 = new TcpClient();
            //TcpClient pClient2 = new TcpClient();

            //Assign callbacks for the relevant client events.
            pClient1.SetOnMessageCallback(new MessageCallbackType(simpleEchoCallback));
            pClient1.SetOnConnectCallback(new ConnectCallbackType(connectedCallback));
            pClient1.SetOnAuthenticationCallback(new MessageCallbackType(authenticationCallback));
            pClient1.SetOnChannelsReceivedCallback(new ChannelsCallbackType(channelsReceivedCallback));

            //Starts the connection.
            pClient1.Start(address, port1);

            /*
            pClient2.SetOnMessageCallback(new MessageCallbackType(simpleEchoCallback));
            pClient2.SetOnConnectCallback(new ConnectCallbackType(connectedCallback));
            pClient2.SetOnChannelsReceivedCallback(new ChannelsCallbackType(channelsReceivedCallback));
            pClient2.Start(address, port2);
            */

            //Test queueing of messages before connection occurs.
            //JObject queuedMsg = new JObject();
            //queuedMsg.Add("queued", "packet");
            //pClient1.Write(new Packet(6, queuedMsg));

            //JObject queuedMsg2 = new JObject();
            //queuedMsg2.Add("queued", "channelNameMessage");
            //pClient2.Write("WORKFLOW_HEARTBEAT_CLIENT_REQUEST", queuedMsg2);

            while (true)
            {
                System.Threading.Thread.Sleep(5000);
            }
        }
    }
}